# CROSS=
# 指令编译器和选项
CC=${CROSS}gcc
CPP=${CROSS}g++
CFLAGS=-O3 -Wall -std=gnu99
EXE=
DLL=dll
USE_LFS=1
USE_LUASOCKET=1
USE_ZLIB=0
USE_LSQLITE=1
HOST_RM= rm -f
ifeq (Windows,$(findstring Windows,$(OS))$(MSYSTEM)$(TERM))
  HOST_SYS= Windows
  HOST_RM= del
else
  HOST_SYS:= $(shell uname -s)
  ifneq (,$(findstring MINGW,$(HOST_SYS)))
    HOST_SYS= Windows
    HOST_MSYS= mingw
  endif
  ifneq (,$(findstring CYGWIN,$(HOST_SYS)))
    HOST_SYS= Windows
    HOST_MSYS= cygwin
  endif
endif
TARGET_SYS?= $(HOST_SYS)
ifeq (Windows,$(TARGET_SYS))
  EXE=.exe
endif

# 目标文件
TARGET_LIB=tolua.${DLL}
SRCS = tolua.c \
int64.c \
uint64.c \
pb.c \
lpeg.c \
struct.c \
cjson/strbuf.c \
cjson/lua_cjson.c \
external/lpack/lpack.c \
cjson/fpconv.c \
external/win/WinBridgeUtils.cpp \
external/win/NativeWin32.cpp

INC = -I./ -I./luajit-2.1/src -I./external/lpack -I./external/win
DLIBS =-lluajit -lws2_32 -static-libgcc -static-libstdc++ -lstdc++
LDFLAGS := -L./luajit-2.1/src -L./window/${TARGET}
ifeq (x86_64,$(TARGET))
	CFLAGS+=-m64
else
	CFLAGS+=-m32
endif

ifeq (1,$(USE_LFS))
	CFLAGS+=-DUSE_LFS=1
	SRCS+=external/lfs/lfs.c
	INC+=-I./external/lfs 
endif

ifeq (1,$(USE_LUASOCKET))
	CFLAGS+=-DUSE_LUASOCKET=1
	SRCS+=luasocket/auxiliar.c \
			luasocket/buffer.c \
			luasocket/except.c \
			luasocket/inet.c \
			luasocket/io.c \
			luasocket/luasocket.c \
			luasocket/mime.c \
			luasocket/options.c \
			luasocket/select.c \
			luasocket/tcp.c \
			luasocket/timeout.c \
			luasocket/udp.c \
			luasocket/wsocket.c 
endif

ifeq (1,$(USE_LSQLITE))
	CFLAGS+=-DUSE_LSQLITE=1
	SRCS+=external/lsqlite3/sqlite3.c external/lsqlite3/lsqlite3.c
	INC+=-I./external/lsqlite3
endif

OBJS =$(patsubst %cpp,%o,$(SRCS:.c=.o))

$(TARGET_LIB):$(OBJS)
	$(CC) -shared -o $@ $^ $(LDFLAGS) $(DLIBS)
test:test_tolua.c
	$(CC)  -o $@${EXE}  $^  $(TARGET_LIB)
clean:
	${HOST_RM}  $(TARGET_LIB) $(OBJS) test
%.o:%.cpp
	$(CPP) $(INC) -o $@ -c $<
%.o:%.c
	$(CC) $(CFLAGS) $(INC) -o $@ -c $<